package com.wen.common.utils;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wen.common.enums.CodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

@Data
@AllArgsConstructor
//@NoArgsConstructor
public class ResponseResult<T> implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(ResponseResult.class);

    private String code;

    private String msg;

    //data这个成员变量的类型为T,T的类型由外部指定
    private T data;

    public ResponseResult() {

    }

    public ResponseResult(T data) {//泛型构造方法形参data的类型也为T，T的类型由外部指定
        this.data = data;
    }

    //请求成功不携带数据方法
    public static ResponseResult success() {
        ResponseResult responseResult = new ResponseResult<>();
        responseResult.setCode(CodeEnum.SUCCESS.getResultCode());
        responseResult.setMsg(CodeEnum.SUCCESS.getResultMsg());
        return responseResult;

    }

    //请求成功携带数据的泛型方法
    public static <T> ResponseResult<T> success(T data) {//泛型方法
        ResponseResult<T> responseResult = new ResponseResult<>(data);
        responseResult.setCode(CodeEnum.SUCCESS.getResultCode());
        responseResult.setMsg(CodeEnum.SUCCESS.getResultMsg());
        return responseResult;

    }

    //失败 定义状态码和显示信息
    public static ResponseResult error(String code, String msg) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(code);
        responseResult.setMsg(msg);
        return responseResult;
    }

    public static ResponseResult error(CodeEnum codeEnum) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(codeEnum.getResultCode());
        responseResult.setMsg(codeEnum.getResultMsg());
        return responseResult;
    }

    public static void printResult(HttpServletResponse response, ResponseResult<?> responseResult) {
        response.setStatus(HttpStatus.OK.value());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try (PrintWriter writer = response.getWriter()) {

            // 1.JSONUtil
            String json1 = JSONUtil.toJsonStr(responseResult);

            // 2. 使用 Gson 将对象转换为 JSON 字符串
            String json2 = JSON.toJSONString(responseResult);

            // 3.ObjectMapper
            ObjectMapper mapper = new ObjectMapper();
            String json3 = mapper.writeValueAsString(responseResult);

            // 将 JSON 字符串写入响应体
            writer.print(json3);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
    public static void printError(HttpServletResponse response, CodeEnum codeEnum) {
        response.setStatus(HttpStatus.OK.value());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try (PrintWriter writer = response.getWriter()) {

            ResponseResult<?> responseResult = ResponseResult.error(codeEnum);

            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(responseResult);

            // 将 JSON 字符串写入响应体
            writer.print(json);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    //test（错误写法）
//    public static T success2(Class<T> c) throws InstantiationException, IllegalAccessException {
//        T t = c.newInstance();
//        return t;
//    }
    //test
    public static <T> T success3(Class<T> c) throws InstantiationException, IllegalAccessException {//泛型方法，声明方法持有一个类型 T（声明T）
        T t = c.newInstance();
        return t;

    }

    /**********************************/

//test
    public T success1(Class<T> c) throws InstantiationException, IllegalAccessException {
        T t = c.newInstance();
        return t;
    }

    public <T> T success4(Class<T> c) throws InstantiationException, IllegalAccessException {//泛型方法，声明方法持有一个类型 T（声明T）
        T t = c.newInstance();
        return t;

    }


    interface Entry<K, V> {
        K getKey();

        V getValue();

        class test {
            interface inter {
            }
        }

    }

    public static class hello {

        interface inter {
        }

    }


}
